/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.filters;

import com.github.sog.config.StringPool;
import org.apache.shiro.web.servlet.ShiroHttpSession;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * remove url JSESSIONID filter.
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-04-11 23:57
 * @since JDK 1.6
 */
@WebFilter(filterName = "sessionIdFilter", value = "/login")
public class SessionIdFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            String path = httpServletRequest.getRequestURL().toString();
            if (path.contains(StringPool.SEMICOLON + ShiroHttpSession.DEFAULT_SESSION_ID_NAME)) {
                ((HttpServletResponse) response).sendRedirect(httpServletRequest.getContextPath() + "/login");
            } else {

                chain.doFilter(request, response);
            }
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
